﻿var uid;
function loadPage(id) {
    uid=id;
    changePage(1)
}
function loadFriendsPage() {
    changeFriendsPage(1)
}
function changeFriendsPage(page) {
    $.ajax({
        url: "./friendsBlogs?page=" + page,
        method: "GET",
        dataType: "json"
    }).done(function (result) {
        //根据取得的博客数量增加页面上的博客显示框
        addItems(result.blogs.length);
        //在博客显示框里添加博客的信息
        updateBlogs(result.blogs);
        //更新页面选择框
        addPageBar(result.pageNum, page,true);
    }).fail(function (xhr, status) {
        console.log(status);
    });
}
function changePage(page) {
    $.ajax({
        url: "./getBlogs?page=" + page+"&" + "user_id=" + uid,
        method: "GET",
        dataType: "json"
    }).done(function (result) {
        //根据取得的博客数量增加页面上的博客显示框
        addItems(result.blogs.length);
        //在博客显示框里添加博客的信息
        updateBlogs(result.blogs);
        //更新页面选择框
        addPageBar(result.pageNum, page,false);
    }).fail(function (xhr, status) {
        console.log(status);
    });
}


function addItems(number) {
    var itemContainer = $(".article-list");
    removeBlogItemsAndPageBar();
    //套用写好的模板，博客信息预览框
    var item =
        "        <div class=\"item\">\n" +
        "            <div class=\"art-img\" >\n" +
        "                <img class='Pic' src=\"/MySpace/resources/default_pic.jpg\"/>\n" +
        "            </div>\n" +
        "            <div class=\"title\">\n" +
        "                <a id='title' ></a>\n" +
        "            </div>\n" +
        "            <div class=\"base-info\">\n" +
        "                <ul>\n" +
        "                    <li>\n" +
        "                        <img src=\"/MySpace/avatar/default.jpg\" class=\"xwcms2\" />\n" +
        "                    </li>\n" +
        "                    <li>\n" +
        "                        <a></a>\n" +
        "                    </li>\n" +
        "                    <li>\n" +
        "                        发布时间： <span></span>\n" +
        "                    </li>\n" +
        "                </ul>\n" +
        "            </div>\n" +
        "            <div class=\"desc\">\n" +
        "                \n" +
        "            </div>\n" +
        "        </div>";
    for (var i = 0; i < number; i++) {
        itemContainer.append(item);
    }
    addSelectContainer();
}

function updateBlogs(blogs) {
    for (var i = 0; i < blogs.length; i++) {
        var item = $(".item").eq(i);
        item.find("a").eq(0).text(blogs[i].blogTitle);
        item.find("a").eq(0).attr("href","./blogText.html?id="+blogs[i].id)
        item.find("a").eq(1).text(blogs[i].author);
        item.find("a").eq(1).attr("href","./moreinfo.html?id="+blogs[i].uid)
        item.find("span").eq(0).text(blogs[i].publishTime);
        item.find(".desc").text(blogs[i].blogIntroduction);
        if (blogs[i].pic != "null") {
            item.find(".Pic").attr("src", "/MySpace/blogsPic/" + blogs[i].pic);
        }
        if (blogs[i].authorAvatar != "null") {
            item.find(".xwcms2").attr("src", "/MySpace/avatar/" + blogs[i].authorAvatar);
        }
    }
}

//删除页面上存在的博客信息预览框和页面选择框
function removeBlogItemsAndPageBar() {
    var items = $(".item");
    if (items != null) {
        for (var i = 0; i < items.length; i++) {
            items[i].remove();
        }
    }
    $(".pageSelector").remove();
}

//添加页面选择框
function addSelectContainer() {
    var itemContainer = $(".article-list");
    var item = "<div class=\"pageSelector\"></div>";
    itemContainer.append(item);
}

//添加页面选择按钮
function addPageBar(pageLength, selectPage,isFriendsPage) {
    var pageSelector = $(".pageSelector");
    var i;
    var k;
    console.log(pageLength);
    if (selectPage - 3 >= 0) {
        i = selectPage - 2;
        k = selectPage + 2;
    } else {
        i = 1;
    }
    if (selectPage + 2 > pageLength) {
        k = pageLength;
        i = pageLength - 5;
    }
    if (pageLength <= 5) {
        i = 1;
        k = pageLength;
    }
    for (i; i <= k; i++) {
        if(isFriendsPage==false)
            var item = "<div class=\"pageBar\"onclick='changePage(" + i + ")'>" + i + "</div>"
        else
            var item = "<div class=\"pageBar\"onclick='changeFriendsPage(" + i + ")'>" + i + "</div>"
        if (i == selectPage)
            item = "<div class=\"pageBar\" style='background: lightblue' onclick='changePage(" + i + ")'>" + i + "</div>"
        pageSelector.append(item);
    }
}


